﻿#include "foeditdialog.h"
#include "ui_foeditdialog.h"
#include <QSqlQuery>
#include <QtDebug>
int FOeditDialog::FID;
FOeditDialog::FOeditDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FOeditDialog)
{
    ui->setupUi(this);
    ui->frame->resize(this->geometry().width(), ui->frame->height());
    ui->status->setText("修改信息状态：");
    ui->dtOutTime->setDate(QDate::currentDate());
}

FOeditDialog::~FOeditDialog()
{delete ui;}

void FOeditDialog::receiveID(int id)
{
    FID=id;
    QSqlQuery query;
    QString strSql = QString("select * from FileOut where FileOutID='%1';").arg(id);
    query.exec(strSql);
    if(query.next()){
        ui->dtOutTime->setDate(QDate::fromString(query.value(1).toString(),"yyyy-MM-dd"));
        ui->edtName->setText(query.value(2).toString());
        if(query.value(3).toInt() == 9){//判断证件类型是否“其他”
            ui->cbBxIDType->setCurrentIndex(query.value(3).toInt()-2);
        }else{
            ui->cbBxIDType->setCurrentIndex(query.value(3).toInt()-1);
        }
        ui->edtID->setText(query.value(4).toString());
        ui->edtPreDuty->setText(query.value(5).toString());
        ui->edtOutToUnit->setText(query.value(6).toString());
        ui->edtDuringPeople->setText(query.value(8).toString());
        ui->txtOutReason->setText(query.value(7).toString());
        ui->txtDescription->setText(query.value(9).toString());
    }
}

void FOeditDialog::on_btnOk_clicked()
{
    strOutTime =  ui->dtOutTime->date().toString("yyyy-MM-dd");//转出时间
    strName = ui->edtName->text();
    if(ui->cbBxIDType->currentIndex() != 7){
        strIDType = QString::number(ui->cbBxIDType->currentIndex()+1);
    }else{
        strIDType = QString::number(ui->cbBxIDType->currentIndex()+2);
    }
    strID = ui->edtID->text();
    strPreDuty = ui->edtPreDuty->text();
    strToUnit = ui->edtOutToUnit->text();
    strOutReason = ui->txtOutReason->toPlainText();
    strDuringPeople = ui->edtDuringPeople->text();
    strRemark = ui->txtDescription->toPlainText();

    if(strName.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入姓名！");
    }else if(strName.size() > 50){
        ui->status->setText("添加信息状态：姓名信息输入有误！");
    }else if(strID.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入证件号！");
    }else if(strID.size() > 18){
        ui->status->setText("添加信息状态：证件号信息输入有误！");
    }else if(strPreDuty.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入原单位职务！");
    }else if(strPreDuty.size() > 80){
        ui->status->setText("添加信息状态：原单位职务信息输入有误！");
    }else if(strToUnit.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入转去单位！");
    }else if(strToUnit.size() > 120){
        ui->status->setText("添加信息状态：转去单位信息输入有误！");
    }else if(strOutReason.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入转出原因！");
    }else if(strOutReason.size() > 500){
        ui->status->setText("添加信息状态：转出原因信息输入有误！");
    }else if(strDuringPeople.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入经办人！");
    }else if(strDuringPeople.size() > 50){
        ui->status->setText("添加信息状态：经办人信息输入有误！");
    }else{
        QString strUpdate=QString("update FileOut set FileOutTime = '%1',FilePerName = '%2',"
                                  "CredTypeID = '%3',IDNum = '%4',OrignalDuty = '%5',"
                                  "FileToUnit = '%6',FileOutRsn = '%7',FileDrgName = '%8',FileOutRmk = '%9'"
                                  "where FileOutID = '%10';")
                .arg(strOutTime).arg(strName).arg(strIDType).arg(strID)
                .arg(strPreDuty).arg(strToUnit).arg(strOutReason).arg(strDuringPeople).arg(strRemark).arg(FID);
        QSqlQuery query;
        if(query.exec(strUpdate)){
            ui->status->setText("修改信息状态：修改成功！");
            this->close();
        }else{
            ui->status->setText("修改信息状态：修改失败！");
        }
    }
}

void FOeditDialog::on_btnCancel_clicked()
{
    this->close();
}
